package com.chris.libs.http.x.v1;

import com.chris.dev.base.libs.v1.http.protocols.HttpHeader;
import com.chris.dev.base.libs.v1.http.protocols.HttpRequest;
import com.chris.libs.devtools.x.v1.utils.JsonUtils;
import com.chris.libs.devtools.x.v1.utils.LoggerUtils;
import com.chris.libs.devtools.x.v1.utils.StorageUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * create by Chris Chan
 * create on 2019/7/9 23:22
 * use for :
 */
public class NetApi {
    public static final String HTTP_HEADER_AUTHORIZATION_BEARER_PRE = "Bearer ";
    public static final String HTTP_HEADER_AUTHORIZATION_BASIC_PRE = "Basic ";
    public static HttpHeader httpHeader = null;
    public static PostHeaderCallback postHeaderCallback;

    /**
     * 生成简单的 RequestBody
     * 请使用 simpleMapRequestBody()
     *
     * @param map
     * @return
     */
    @Deprecated
    public static RequestBody simpleRequestBody(Map<String, Object> map) {
        return simpleMapRequestBody(map);
    }

    /**
     * 生成简单的 RequestBody
     *
     * @param map
     * @return
     */
    public static RequestBody simpleMapRequestBody(Map<String, Object> map) {
        return RequestBody.create(MediaType.parse("application/json"), JsonUtils.objToJson(map));
    }

    /**
     * 生成简单的 RequestBody
     *
     * @param obj
     * @return
     */
    public static RequestBody simpleObjRequestBody(Object obj) {
        return RequestBody.create(MediaType.parse("application/json"), JsonUtils.objToJson(obj));
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> RequestBody postRequestBody(String token, T obj) {
        HttpRequest<T> httpRequest = getHttpRequest(token);
        // 业务干预
        if (null != NetApi.postHeaderCallback) {
            NetApi.postHeaderCallback.post(NetApi.httpHeader);
        }
        String requestBodyJson = httpRequest.setParam(obj).build();
        LoggerUtils.d("Request JSON ==> " + requestBodyJson);
        return RequestBody.create(MediaType.parse("application/json"), requestBodyJson);
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param obj
     * @param postHeaderCallback
     * @param <T>
     * @return
     */
    public static <T> RequestBody postRequestBody(String token, T obj, PostHeaderCallback postHeaderCallback) {
        HttpRequest<T> netRequest = getHttpRequest(token);
        if (null != postHeaderCallback) {
            postHeaderCallback.post(netRequest.getHeader());
        }
        String build = netRequest.setParam(obj).build();
        LoggerUtils.d("Request JSON ==> " + build);
        return RequestBody.create(MediaType.parse("application/json"), build);
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param map
     * @return
     */
    public static RequestBody postRequestMapBody(String token, Map<String, Object> map) {
        return postRequestBody(token, map);
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param map
     * @param postHeaderCallback
     * @return
     */
    public static RequestBody postRequestMapBody(String token, Map<String, Object> map, PostHeaderCallback postHeaderCallback) {
        return postRequestBody(token, map, postHeaderCallback);
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param obj
     * @return
     */
    public static <T> RequestBody postRequestObjBody(String token, T obj) {
        return postRequestBody(token, obj);
    }

    /**
     * 生成post请求数据
     *
     * @param token
     * @param obj
     * @param postHeaderCallback
     * @return
     */
    public static <T> RequestBody postRequestObjBody(String token, T obj, PostHeaderCallback postHeaderCallback) {
        return postRequestBody(token, obj, postHeaderCallback);
    }

    /**
     * 生成
     *
     * @param map
     * @return
     */
    @Deprecated
    public static Map<String, Object> requestMap(Map<String, Object> map) {
        Map<String, Object> hMap = new HashMap<>();
        hMap.put("header", getRequestMap());
        hMap.put("param", map);

        return hMap;
    }

    /**
     * 构建Bearer头
     *
     * @param sharedPreferencesKey
     * @param defValue
     * @return
     */
    public static String buildBearerAuthorization(String sharedPreferencesKey, String defValue) {
        return HTTP_HEADER_AUTHORIZATION_BEARER_PRE + StorageUtil.read(sharedPreferencesKey, defValue);
    }

    /**
     * 构建Basic头
     *
     * @param sharedPreferencesKey
     * @param defValue
     * @return
     */
    public static String buildBasicAuthorization(String sharedPreferencesKey, String defValue) {
        return HTTP_HEADER_AUTHORIZATION_BASIC_PRE + StorageUtil.read(sharedPreferencesKey, defValue);
    }

    @Deprecated
    public static HttpRequest<?> getRequestBody() {
        return new HttpRequest<>();
    }

    /**
     * 初始化协议
     * 填写协议固定部分
     * 需要在application中执行一次
     */
    public static void initHeader(HttpHeader httpHeader) {
        NetApi.httpHeader = httpHeader;
    }

    /**
     * 初始化协议
     * 填写协议固定部分
     * 需要在application中执行一次
     */
    public static void initHeader(HttpHeader httpHeader, PostHeaderCallback postHeaderCallback) {
        NetApi.httpHeader = httpHeader;
        NetApi.postHeaderCallback = postHeaderCallback;
    }

    /**
     * 获取默认的协议
     *
     * @return
     */
    public static HttpHeader getDefaultHttpHeader() {
        if (null == NetApi.httpHeader) {
            throw new RuntimeException("This default is not initialized");
        }
        return NetApi.httpHeader;
    }

    public static <T> HttpRequest<T> getHttpRequest(HttpHeader httpHeader, String token, Long lng, Long lat) {
        httpHeader.setToken(token).setLng(lng).setLat(lat);
        return new HttpRequest<>(httpHeader);
    }

    /**
     * 获取带协议的请求体
     *
     * @param token
     * @param lng
     * @param lat
     * @return
     */
    public static <T> HttpRequest<T> getHttpRequest(String token, Long lng, Long lat) {
        return getHttpRequest(getDefaultHttpHeader(), token, lng, lat);
    }

    public static <T> HttpRequest<T> getHttpRequest(String token) {
        return getHttpRequest(token, null, null);
    }

    @Deprecated
    public static Map<String, Object> getRequestMap() {
        return new HashMap<>(16);
    }

    /**
     * 构建一个json格式的请求体
     *
     * @param content
     * @return
     */
    public static RequestBody buildJsonRequestBody(String content) {
        return RequestBody.create(MediaType.parse("application/json"), content);
    }

    // 业务干预header的处理
    public interface PostHeaderCallback {
        void post(HttpHeader header);
    }
}
